package com.tf_cut.backend.seller.service;


import com.tf_cut.backend.common.pojo.PageInfo;
import com.tf_cut.backend.common.pojo.Result;
import com.tf_cut.backend.common.service.CommonServiceImpl;
import com.tf_cut.backend.mapper.SellerBillMapper;
import com.tf_cut.backend.seller.pojo.SellerBill;
import com.tf_cut.backend.seller.repository.SellerBillRepository;
import com.tf_cut.backend.seller.vo.SellerBillAndConsigneeVo;
import com.tf_cut.backend.seller.vo.SellerOrderVo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

@Service
@Transactional
@Slf4j
public class SellerBillServiceImpl extends CommonServiceImpl<SellerBillAndConsigneeVo, SellerBill, String> implements SellerBillService {

    @PersistenceContext
    private EntityManager em;
    @Autowired
    private SellerBillRepository sellerBillRepository;
    @Autowired
    private SellerConsigneeService sellerConsigneeService;
    @Autowired
    private SellerBillMapper sellerBillMapper;


    @Override
    public Result<SellerBillAndConsigneeVo> insertBillAndConsignee(SellerBillAndConsigneeVo avo) {

        Result<SellerBillAndConsigneeVo> result = sellerConsigneeService.save(avo);//保存收件人

        //保存收件人id
        avo.setRecivePersonName(result.getData().getRecivePersonId());

        result = this.save(avo);    // 新建bill


        result.setData(avo);

        return result;
    }

    @Override
    public PageInfo<SellerBillAndConsigneeVo> querySellerBillList(SellerBillAndConsigneeVo sellerBillAndConsigneeVo) {
        com.github.pagehelper.PageHelper.startPage(sellerBillAndConsigneeVo.getPage(), sellerBillAndConsigneeVo.getRows());
        List<SellerBillAndConsigneeVo> list = sellerBillMapper.querySellerBillList(sellerBillAndConsigneeVo);
        com.github.pagehelper.PageInfo<SellerBillAndConsigneeVo> pageInfo = new com.github.pagehelper.PageInfo<>(list);

        PageInfo<SellerBillAndConsigneeVo> returnPageInfo = new PageInfo<>();
        returnPageInfo.setPage(pageInfo.getPageNum());
        returnPageInfo.setPageSize(pageInfo.getPageSize());
        returnPageInfo.setRows(pageInfo.getList());
        returnPageInfo.setRecords((int) pageInfo.getTotal());
        returnPageInfo.setTotal(pageInfo.getPages());
        return returnPageInfo;

    }

	@Override
	public PageInfo<SellerBillAndConsigneeVo> queryBilling(int page, int rows, String sellerCompanyName, String status,
			String startDate, String endDate) {
		com.github.pagehelper.PageHelper.startPage(page,rows);
		if(!startDate.equals("")){
			startDate=startDate+" 00:00:00";
		}
		if(!endDate.equals("")){
			endDate=endDate+" 23:59:59";
		}
        List<SellerBillAndConsigneeVo> list = sellerBillMapper.queryBilling(sellerCompanyName,status,startDate,endDate);
        com.github.pagehelper.PageInfo<SellerBillAndConsigneeVo> pageInfo = new com.github.pagehelper.PageInfo<>(list);
		  PageInfo<SellerBillAndConsigneeVo> returnPageInfo = new PageInfo<>();
	        returnPageInfo.setPage(pageInfo.getPageNum());
	        returnPageInfo.setPageSize(pageInfo.getPageSize());
	        returnPageInfo.setRows(pageInfo.getList());
	        returnPageInfo.setRecords((int) pageInfo.getTotal());
	        returnPageInfo.setTotal(pageInfo.getPages());
	        return returnPageInfo;
	}

	@Override
	public List<SellerBillAndConsigneeVo> findBybill(String sellerCompanyName, String status, String startDate,
			String endDate) {
		
		if(!startDate.equals("")){
			startDate=startDate+" 00:00:00";
		}
		if(!endDate.equals("")){
			endDate=endDate+" 23:59:59";
		}
		
		return sellerBillMapper.findBybill(sellerCompanyName,status,startDate,endDate);
	}
}
